package org.yhtch.jswtdic;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;
//import org.yhtch.util.HtmlEntityComplexChar;

public class JDicUI {

	/**
	 * @param args
	 */
	public static void main(String[] args){
		final JDicCore dicCore=new JDicCore();
		
		final Display display = new Display();
		final Shell shell = new Shell(display,SWT.MIN | SWT.CLOSE | SWT.TITLE | SWT.BORDER);
		
		shell.setText("JSWTDictionary");
		shell.setImage(new Image(display, "res/logo.png"));
		shell.setLayout(new FillLayout());
		shell.addShellListener(new ShellAdapter() {
			public void shellIconified(ShellEvent e) {
				// System.out.println("ok");
				shell.setVisible(false);
			}
			public void shellClosed(ShellEvent e) {
				//super.shellActivated(e);
				dicCore.close();
			}
		});
		
		final Tray tray = display.getSystemTray ();
		if (tray == null) {
			// System.out.println ("The system tray is not available");
		} else {
			final TrayItem item = new TrayItem (tray, SWT.NONE);
			item.setToolTipText("JDic");
			item.addListener (SWT.DefaultSelection, new Listener () {
				public void handleEvent (Event event) {
					if(shell.getVisible()) {
						shell.setVisible(false);
					}else {
						shell.setVisible(true);
						shell.setMinimized(false);
						shell.forceActive();
					}
				}
			});
			item.setImage(new Image(display, "res/logo.png"));
			final Menu trayMenu = new Menu (shell, SWT.POP_UP);
			final MenuItem trayMenuItem0 = new MenuItem(trayMenu, SWT.CHECK);
			trayMenuItem0.setText("Search Net");
			trayMenuItem0.setImage(new Image(display, "res/net.png"));
			trayMenuItem0.setSelection(dicCore.isFindFromNet());
			trayMenuItem0.addListener(SWT.Selection, new Listener() {
				public void handleEvent (Event event) {
					//trayMenuItem0.setSelection(!trayMenuItem0.getSelection());
					dicCore.setFindFromNet(trayMenuItem0.getSelection());
				}
			});
			final MenuItem trayMenuItem1 = new MenuItem (trayMenu, SWT.PUSH);
			trayMenuItem1.setText("Show/Hide");
			trayMenuItem1.setImage(new Image(display, "res/show.png"));
			trayMenuItem1.addListener (SWT.Selection, new Listener () {
				public void handleEvent (Event event) {
					if(shell.getVisible()) {
						shell.setVisible(false);
					}else {
						shell.setVisible(true);
						shell.setMinimized(false);
						shell.forceActive();
					}
				}
			});
			final MenuItem trayMenuItem2 = new MenuItem (trayMenu, SWT.PUSH);
			trayMenuItem2.setText("Exit");
			trayMenuItem2.setImage(new Image(display, "res/exit.png"));
			trayMenuItem2.addListener (SWT.Selection, new Listener () {
				public void handleEvent (Event event) {
					shell.close();
					display.dispose();
				}
			});
			item.addListener (SWT.MenuDetect, new Listener () {
				public void handleEvent (Event event) {
					trayMenu.setVisible (true);
				}
			});
		}
		
		final TabFolder tabFolder = new TabFolder (shell, SWT.BORDER);
		
		TabItem searchItem = new TabItem (tabFolder, SWT.NONE);
		searchItem.setText("Search Word");
		Composite searchComposite=new Composite(tabFolder, SWT.NONE);
		GridLayout layout0=new GridLayout ();
		layout0.numColumns=1;
		//layout0.pack=false;
		//layout0.justify=true;
		// layout0.wrap=true;
		// layout0.justify=true;
		searchComposite.setLayout(layout0);
		//System.out.println(searchComposite.getClientArea().width);
		final Text wordInput = new Text (searchComposite, SWT.BORDER|SWT.WRAP);
		GridData gd0 = new GridData();
		gd0.horizontalAlignment=GridData.FILL;
		gd0.grabExcessHorizontalSpace=true;
		wordInput.setLayoutData(gd0);
		wordInput.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
		final Text explainShow = new Text (searchComposite, SWT.READ_ONLY|SWT.BORDER|SWT.WRAP);
		// final Label explainShow = new Label(searchComposite,
		// SWT.BORDER|SWT.SINGLE|SWT.WRAP);
		GridData gd1 = new GridData();
		gd1.horizontalAlignment=GridData.FILL;
		gd1.grabExcessHorizontalSpace = true;
		gd1.verticalAlignment=GridData.FILL;
		gd1.grabExcessVerticalSpace=true;
		explainShow.setLayoutData(gd1);
		//explainShow.setText(HtmlEntityComplexChar.decode("[ 'pikt&#643;&#601; ]"));
		//explainShow.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
		wordInput.addModifyListener (new ModifyListener () {
			public void modifyText (ModifyEvent event) {
				// text1.setTopIndex (text0.getTopIndex ());
				// text1.setSelection (event.start, event.end);
				String word=wordInput.getText().trim();
				if("".equals(word)||null==word) {
					explainShow.setText("");
					return;
				}
				String explain=dicCore.explainWord(word);
				if(null!=explain)
					explainShow.setText(explain);
				else
					explainShow.setText("Not Find!");
			}
		});
		searchItem.setControl(searchComposite);
		
		TabItem addItem = new TabItem (tabFolder, SWT.NONE);
		addItem.setText("Add Word");
		Composite addComposite=new Composite(tabFolder, SWT.NONE);
		FillLayout layout1=new FillLayout ();
		layout1.type=SWT.VERTICAL;
		addComposite.setLayout(layout1);
		/*
		 * final Text text2 = new Text (addComposite, SWT.BORDER|SWT.SINGLE);
		 * final Text text3 = new Text (addComposite, SWT.BORDER|SWT.READ_ONLY);
		 * text2.addModifyListener (new ModifyListener () { public void
		 * modifyText (ModifyEvent event) { text2.setTopIndex (text0.getTopIndex
		 * ()); //text1.setSelection (event.start, event.end); String
		 * word=text0.getText(); String explain=dicCore.explainWord(word);
		 * if(null!=explain) text3.setText(explain); else text3.setText(""); }
		 * });
		 */
		addItem.setControl(addComposite);
		
		TabItem aboutItem = new TabItem (tabFolder, SWT.NONE);
		aboutItem.setText("About");
		Composite aboutComposite=new Composite(tabFolder, SWT.NONE);
		FillLayout layout2=new FillLayout ();
		layout2.type=SWT.VERTICAL;
		aboutComposite.setLayout(layout2);
		Link link = new Link(aboutComposite, SWT.NONE);
		link.setText("JSWTDictionary is a swt dictionary written by <a href=\"http://xmxsuperstar.spaces.live.com\">YHTCH</a>");
		link.addSelectionListener(new SelectionAdapter(){ 	   
			public void widgetSelected(SelectionEvent event){
				/*try {
					//Runtime.getRuntime().exec(event.text);
				} catch (IOException e) {
					e.printStackTrace();
				}*/
			}
		});
		aboutItem.setControl(aboutComposite);
		
		tabFolder.pack();
		shell.setBounds(display.getClientArea().width/2-160,
				display.getClientArea().height/2-120,
				320, 240);
		shell.open ();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) display.sleep();
		}
		display.dispose();
	}

}
